package de.koller.planet2D.frame.uv;

import de.koller.oceanFramework.OceanException;
import de.koller.planet2D.frame.previewRender.CameraSteerListener;
import de.koller.planet2D.util.Parsers;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;

public class UVRenderer extends JPanel implements MouseMotionListener, MouseInputListener {
	
	private final UVDesigner designer;
	
	private boolean down = false;
	private int selectBoxX, selectBoxY, selectBoxW, selectBoxH;
	
	public UVRenderer( UVDesigner designer ) {
		this.designer = designer;
		if( designer != null ) {
			CameraSteerListener csl = new CameraSteerListener( designer.camera );
			this.addMouseMotionListener( csl );
			this.addMouseWheelListener( csl );
			this.addMouseListener( csl );
			
			this.addMouseMotionListener( this );
			this.addMouseListener( this );
		}
	}

	@Override
	protected void paintComponent( Graphics g ) {
		super.paintComponent( g );
		
		Graphics2D g2 = (Graphics2D)g;
		updateSelectbox();
		
		designer.camera.preTransform( g2 );
		g2.drawImage( designer.proot.renderContainer.textur, 0, 0, null );
		
		g2.setStroke( new BasicStroke( 0 ) );
	
		if( down ) {
			g2.setColor( Color.BLUE );
			g2.fillRect( selectBoxX, selectBoxY, selectBoxW, selectBoxH );
		}
		
		g2.setColor( Color.RED );
		g2.drawRect( selectBoxX, selectBoxY, selectBoxW, selectBoxH );
		
		designer.camera.postTransform( g2 );
	}
	
	public void updateSelectbox() {
		selectBoxX = (int)Parsers.getValue( selectBoxX, designer.comp.x.getText() );
		selectBoxY = (int)Parsers.getValue( selectBoxY, designer.comp.y.getText() );
		selectBoxW = (int)Parsers.getValue( selectBoxW, designer.comp.w.getText() );
		selectBoxH = (int)Parsers.getValue( selectBoxH, designer.comp.h.getText() );
	}

	@Override
	public void mouseDragged( MouseEvent e ) {
		if( !down )
			return;
		
		int[] p = getMousePos( e );
		designer.comp.w.setText( (p[0]-selectBoxX) + "" );
		designer.comp.h.setText( (p[1]-selectBoxY) + "" );
		
		this.repaint();
	}

	@Override
	public void mousePressed( MouseEvent e ) {
		if( e.getButton() != 1 )
			return;
		
		int[] p = getMousePos( e );
		designer.comp.x.setText( p[0] + "" );
		designer.comp.y.setText( p[1] + "" );
		designer.comp.w.setText( "0" );
		designer.comp.h.setText( "0" );
		down = true;
		
		this.repaint();
	}

	@Override
	public void mouseReleased( MouseEvent e ) {
		down = false;
	}

	@Override public void mouseMoved( MouseEvent e ) {}
	@Override public void mouseClicked( MouseEvent e ) {}
	@Override public void mouseEntered( MouseEvent e ) {}
	@Override public void mouseExited( MouseEvent e ) {}	

	private int[] getMousePos( MouseEvent e ) {
		double x = 0;
		double y = 0;

		try {
			Point2D tr = designer.camera.getUsedTransform().inverseTransform( e.getPoint(), null );
			x = tr.getX();
			y = tr.getY();
		} catch ( NoninvertibleTransformException ex ) {
			assert true;
		}

		return new int[] { (int)x, (int)y };
	}
}
